package cn.wxz1997.permission.config;

import cn.wxz1997.permission.dao.UserMapper;
import cn.wxz1997.permission.model.User;
import cn.wxz1997.permission.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @Description: 根据用户名查找用户信息，并封装到UserDetails中
 * @Author: wxz1997
 * @Date 18-8-5下午4:44
 */
@Slf4j
@Component
public class MyUserDetailsService implements UserDetailsService{

    @Autowired
    private UserService userService;

    /**
     * 根据用户名获取用户信息
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("登录用户名：" + s);
        User user = userService.selectByUsername(s);
        if (user == null){
            log.info("用户名不存在！");
            throw new UsernameNotFoundException("用户名不存在！");
        }
        return user;
    }
}
